package com.reremouse.rerebbs.dao.impl;

import com.reremouse.rerebbs.dao.IBbsZoneDAO;
import com.reremouse.rerebbs.model.BbsThread;
import com.reremouse.rerebbs.model.BbsZone;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.List;

public class BbsZoneDAOImpl
        implements IBbsZoneDAO, Serializable {
    @PersistenceContext
    private EntityManager entityManager;
    public static final String NAME = "name";
    public static final String DESCRIPTION = "description";
    public static final String MANAGER = "manager";
    public static final String ORDER_INDEX = "orderIndex";

    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void save(BbsZone entity) {
        try {
            this.entityManager.persist(entity);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public void delete(long id) {
        try {
            BbsZone entity = (BbsZone) this.entityManager.getReference(BbsZone.class, Long.valueOf(id));

            this.entityManager.remove(entity);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public BbsZone update(BbsZone entity) {
        try {
            return (BbsZone) this.entityManager.merge(entity);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public BbsZone findById(Long id) {
        try {
            return (BbsZone) this.entityManager.find(BbsZone.class, id);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public List<BbsZone> findByProperty(String propertyName, Object value, int... rowStartIdxAndCount) {
        try {
            String queryString = "select model from BbsZone model where model." + propertyName + "= :propertyValue";

            Query query = this.entityManager.createQuery(queryString);
            query.setParameter("propertyValue", value);
            if ((rowStartIdxAndCount != null) && (rowStartIdxAndCount.length > 0)) {
                int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
                if (rowStartIdx > 0) {
                    query.setFirstResult(rowStartIdx);
                }
                if (rowStartIdxAndCount.length > 1) {
                    int rowCount = Math.max(0, rowStartIdxAndCount[1]);
                    if (rowCount > 0) {
                        query.setMaxResults(rowCount);
                    }
                }
            }
            return query.getResultList();
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public List<BbsZone> findByName(Object name, int... rowStartIdxAndCount) {
        return findByProperty("name", name, rowStartIdxAndCount);
    }

    public List<BbsZone> findByDescription(Object description, int... rowStartIdxAndCount) {
        return findByProperty("description", description, rowStartIdxAndCount);
    }

    public List<BbsZone> findByManager(Object manager, int... rowStartIdxAndCount) {
        return findByProperty("manager", manager, rowStartIdxAndCount);
    }

    public List<BbsZone> findByOrderIndex(Object orderIndex, int... rowStartIdxAndCount) {
        return findByProperty("orderIndex", orderIndex, rowStartIdxAndCount);
    }

    public List<BbsZone> findAll(int... rowStartIdxAndCount) {
        try {
            String queryString = "select model from BbsZone model order by model.orderIndex";
            Query query = this.entityManager.createQuery("select model from BbsZone model order by model.orderIndex");
            if ((rowStartIdxAndCount != null) && (rowStartIdxAndCount.length > 0)) {
                int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
                if (rowStartIdx > 0) {
                    query.setFirstResult(rowStartIdx);
                }
                if (rowStartIdxAndCount.length > 1) {
                    int rowCount = Math.max(0, rowStartIdxAndCount[1]);
                    if (rowCount > 0) {
                        query.setMaxResults(rowCount);
                    }
                }
            }
            return query.getResultList();
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public BbsThread findLatestThread(long id) {
        String q = "select bt from BbsThread bt where bt.zone.id=" + id + " order by bt.latestTalk.genTime desc";
        List<BbsThread> ps = this.entityManager.createQuery(q).getResultList();
        if (!ps.isEmpty()) {
            return (BbsThread) ps.get(0);
        }
        return null;
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.dao.impl.BbsZoneDAOImpl

 * JD-Core Version:    0.7.0.1

 */